// 认证相关工具函数

// 检查是否已登录
export const isLoggedIn = () => {
  const token = uni.getStorageSync('token')
  const userInfo = uni.getStorageSync('userInfo')
  return !!(token && userInfo)
}

// 获取当前用户信息
export const getCurrentUser = () => {
  return uni.getStorageSync('userInfo') || null
}

// 获取token
export const getToken = () => {
  return uni.getStorageSync('token') || ''
}

// 清除登录信息
export const clearAuth = () => {
  uni.removeStorageSync('token')
  uni.removeStorageSync('userInfo')
}

// 跳转到登录页
export const redirectToLogin = () => {
  uni.reLaunch({
    url: '/pages/login/index'
  })
}

// 检查登录状态，未登录则跳转到登录页
export const checkAuth = () => {
  if (!isLoggedIn()) {
    redirectToLogin()
    return false
  }
  return true
}

// 登出
export const logout = () => {
  clearAuth()
  uni.showToast({
    title: '已退出登录',
    icon: 'success',
    duration: 1500
  })
  setTimeout(() => {
    redirectToLogin()
  }, 1500)
}
